如何在Jupyter Notebook中Python2和Python3内核共存 您所在的位置:网站首页 python2与python3 导入模块区别 如何在Jupyter Notebook中Python2和Python3内核共存

如何在Jupyter Notebook中Python2和Python3内核共存

2023-11-24 04:11| 来源: 网络整理| 查看: 265

 

如果还没有安装Python2和Python3的可以移步另一篇“如何在win10中共存Python2和Python3”

 

一、pip安装Jupyter Notebook

 

这里两个版本安装上jupyter notebook

 

python3 -m pip install --upgrade pip --force-reinstall

python3 -m pip install jupyter

 

python2 -m pip install --upgrade pip --force-reinstall

python2 -m pip install jupyter

 

安装成功后直接运行

 

jupyter notebook

 

    这里有一个小技巧可以分享一下:我们每次启动jupyter notebook都需要打开在cmd输入jupyter notebook,尤其是有时候还需要cd进某文件夹。

    因此,可以新建一个txt文件,用以下语句写一个BAT脚本,保存为 jupyter notebook.bat

rem -- start_jupyter_notebook.bat ---

jupyter notebook

pause

 

    将该BAT文件放在你想放置的文件位置中,再将文件的快捷方式发送至桌面,就可以方便启动了!  

    启动jupyter notebook之后,可以看到只有一个kernel存在。

二、为Jupyter Notebook添加多个python内核

 

1、当我们需要python双版本时,就需要添加该python版本的内核:

 

查看jupyter notebook内核列表

jupyter kernelspec list

安装或删除其他内核

ipython kernel install --name python2   #安装python2  

jupyter kernelspec uninstall python2   #删除python2  

 

2、上面的内核安装操作完成后,便能在jupyter notebook上出现多个内核切换

 

 

3、下面进行验证为否成功添加:

 

Python2

 

Python3

 

4、if( 验证成功 )

        恭喜,配置结束!

        break;

 

5、if( 验证不成功 )

        恭喜,继续配置 ... ...

 

    1)根据上面安装过程中的经验可知,在安装内核的时候实际上是把kernel文件复制到了这个文件目录下

C:\ProgramData\jupyter\kernels

 

    2)所以,我的思路是软的不行就来硬的吧!!!!

    先打开python2文件夹,对里面的kernel.json文件进行编辑:

 

{

"display_name": "Python 2",

"language": "python",

"argv": [

  "c:\\program files (x86)\\microsoft visual studio\\shared\\python27_64\\python2.exe",

  "-m",

  "ipykernel_launcher",

  "-f",

  "{connection_file}"

]

}

    注意:

(1)"display_name": "Python 2",就是在jupyter notebook上显示的python2的名字,这个你开心就好随意修改;

(2)"language": "python" 表示使用python语言,请保持不变;

(3)"c:\\program files (x86)\\microsoft visual studio\\shared\\python27_64\\python2.exe"这是我个人的python2的安装和应用程序路径,你需要根据自己的相关环境变量修改!!!!!!!

    最后,复制+粘贴+保存!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

    同理可得,返回去打开python3文件夹,对里面的kernel.json文件进行编辑

{

"display_name": "Python 3",

"language": "python",

"argv": [

  "c:\\program files (x86)\\microsoft visual studio\\shared\\python36_64\\python3.exe",

  "-m",

  "ipykernel_launcher",

  "-f",

  "{connection_file}"

]

}

    注意:

(1)"display_name": "Python 3",就是在jupyter notebook上显示的python3的名字,这个你开心就好随意修改;

(2)"language": "python" 表示使用python语言,请保持不变;

(3)"c:\\program files (x86)\\microsoft visual studio\\shared\\python36_64\\python3.exe"这是我个人的python2的安装和应用程序路径,你需要根据自己的相关环境变量修改!!!!!!!

    最后,复制+粘贴+保存!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

三、重新启动jupyter notebook,大功告成。

 

 

 

 

 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有